<template>
  <div class="news-container w1200">
    <ul class="news-list flex flex-wrap">
      <li v-for="item in newList" :key="item._id">
        <router-link :to="`/detail/${item._id}`">
          <img :src="item.cover" width="390" height="220" alt="new" />
          <h3 class="ellipsis">{{ item.title }}</h3>
          <div class="date">{{ item.updatedAt }}</div>
        </router-link>
      </li>
    </ul>

    <el-pagination
      background
      small
      v-model:current-page="paginationConfig.pageNumber"
      v-model:page-size="paginationConfig.pageSize"
      :page-sizes="[1, 2, 3]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paginationConfig.total"
      style="justify-content: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getNewslistData } from '@/api/news'
defineOptions({ name: 'NewsList' })
import { useRouter, onBeforeRouteUpdate } from 'vue-router'
const router = useRouter()
const newList = ref([])
const paginationConfig = ref({
  total: 0,
  pageSize: 2,
  pageNumber: 1
})

const getNewslist = async () => {
  const { data, pagination } = await getNewslistData({
    pageNumber: paginationConfig.value.pageNumber,
    pageSize: paginationConfig.value.pageSize
  })
  newList.value = data
  paginationConfig.value = pagination
}

const handleCurrentChange = (val) => {
  paginationConfig.value.pageNumber = val
  router.push({
    path: '/news',
    query: { pageNumber: val, pageSize: paginationConfig.value.pageSize }
  })
}
const handleSizeChange = (val) => {
  paginationConfig.value.pageSize = val
  router.push({
    path: 'news',
    query: { pageNumber: paginationConfig.value.pageNumber, pageSize: val }
  })
}

getNewslist()

onBeforeRouteUpdate((to) => {
  console.log('组件内的守卫:beforeRouteUpdate:', to.query)
  const pageSize = paginationConfig.value.pageSize
  paginationConfig.value.pageNumber = parseInt(to.query.pageNumber) || 1
  paginationConfig.value.pageSize = parseInt(to.query.pageSize) || pageSize
  getNewslist()
})
</script>

<style lang="scss" scoped>
.news-container {
  padding: 80px 0 50px;
  :deep(.el-pagination.is-background .el-pager li) {
    border-radius: 50%;
    width: 35px;
    height: 35px;
    line-height: 35px;
    &:hover {
      background-color: #ff7200;
      color: #fff;
    }
  }
  :deep(.el-pagination) {
    line-height: 35px;
    & > * {
      vertical-align: middle;
    }
    .btn-prev,
    .btn-next {
      border-radius: 50%;
      width: 35px;
      height: 35px;
    }
  }
}

.news-list {
  min-height: 325px;
  padding-bottom: 90px;
  li {
    margin: 0 20px 70px 0;
    width: calc((100% - 40px) / 3);
    &:nth-child(3n) {
      margin-right: 0;
    }
    &:hover {
      h3 {
        color: #ff7200;
      }
    }
    img {
      width: 390px;
      height: 220px;
    }
    h3 {
      font-size: 16px;
      margin-top: 20px;
      padding-left: 20px;
      width: 390px;
      color: #333;
    }
    .date {
      padding-left: 20px;
      margin-top: 30px;
      font-size: 14px;
      color: #666;
    }
  }
}
</style>
